define("apm/ui/business_time_ranges/select", ["require"], function(a) {
	"use strict";
	var b = Ember.Select.extend(Ember.TargetActionSupport, {
		content: Ember.A([{
			name: "前1分钟",
			value: "1m"
		}, {
			name: "前1小时",
			value: "1h"
		}, {
			name: "前1天",
			value: "1d"
		}, {
			name: "前1周",
			value: "7d"
		}, {
			name: "前1个月",
			value: "1M"
		}, {
			name: "当前1天",
			value: "n1d"
		}, {
			name: "当前1个月",
			value: "n1M"
		}]),
		optionLabelPath: "content.name",
		optionValuePath: "content.value",
		targetObject: Ember.computed(function(a) {
			var b = this._parentView;
			return b ? Ember.get(b, "controller") : null
		}).property("_parentView")
	});
	return b
}), define("broada/base/text!apm/ui/business_time_ranges/template.hbs", [], function() {
	return '<div class="input-prepend">\r\n	<button class="btn" {{action "showStartDateSelector"}}>{{btnTxt}}</button>\r\n	{{view view.Select\r\n	id="timeRangeSelect"\r\n	value=selectVal}}\r\n</div>\r\n<div class="start-date-selector">\r\n	<div class="calendar"></div>\r\n	<div class="ui-datepicker-header ui-widget-header ui-datepicker-header ui-helper-clearfix ui-corner-all time-range-hour-x" role="heading">时</div>\r\n    <div class="ui-datepicker-header ui-widget-header ui-datepicker-header ui-helper-clearfix ui-corner-all time-range-minute-x" role="heading">分</div>\r\n    <ul class="dropdown-menu hour-selector">\r\n	{{#each hours as |hour|}}\r\n		<li><a href="#" {{action "hourChange" hour}}>{{hour}}</a></li>\r\n	{{/each}}\r\n	</ul>\r\n	<ul class="dropdown-menu minute-selector">\r\n	{{#each minutes as |minute|}}\r\n		<li><a href="#" {{action "minuteChange" minute}}>{{minute}}</a></li>\r\n	{{/each}}\r\n	</ul>\r\n	<div class="bottombar">\r\n		<div class="pull-left">{{selectedDateFormatted}}</div>\r\n		<div class="pull-right">\r\n			<button class="btn btn-primary" {{action "btnOkAction"}}>确定</button>\r\n			<button class="btn" {{action "btnLastAction"}}>最新</button>\r\n		</div>\r\n	</div>\r\n</div>'
}), define("apm/ui/business_time_ranges/main", ["require", "./select", "broada/wijmo/wijcalendar/jquery.wijmo.wijcalendar", "broada/base/text!./template.hbs"], function(a) {
	"use strict";
	var b = a("broada/base/text!./template.hbs"),
		c = Ember.Component.extend({
			didInsertElement: function() {
				var a = this;
				this.$().find(".hour-selector, .minute-selector").on("click", "li", function(a) {
					$(a.delegateTarget).find("li").removeClass("active"), $(this).addClass("active")
				}), $(document).on("click." + this.elementId, function(b) {
					0 == $(b.target).closest("#" + a.elementId).length && a.send("hideStartDateSelector")
				}), this.setDefaultDate()
				this.set("selectVal","1m");
			},
			willDestroyElement: function() {
				this._destroyCalendar(), $(document).off("click." + this.elementId);
			},
			layout: Ember.HTMLBars.compile(b),
			classNames: "business-time-range",
			Select: a("./select"),
			maxHour: 23,
			hours: Ember.A([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]),
			maxHourChange: Ember.observer("maxHour", function() {
				this.$().find(".hour-selector>li").removeClass("hide"), this.$().find(".hour-selector>li:gt(" + this.get("maxHour") + ")").addClass("hide")
			}),
			maxMinute: 59,
			minutes: Ember.A([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]),
			maxMinuteChange: Ember.observer("maxMinute", function() {
				this.$().find(".minute-selector>li").removeClass("hide"), this.$().find(".minute-selector>li:gt(" + this.get("maxMinute") + ")").addClass("hide")
			}),
			selectedDateChange: Ember.observer("selectedDate", function() {
				var a = this.get("selectedDate");
				if (a) {
					this.set("selectedHour", a.getHours()), this.set("selectedMinute", a.getMinutes());
					var b = APM.util.formatDate(a, "f");
					this.set("selectedDateFormatted", b)
				} else this.set("selectedHour", null), this.set("selectedMinute", null), this.set("selectedDateFormatted", null);
				this.updateHours()
			}),
			selectedHourChange: Ember.observer("selectedHour", function() {
				var a = this.get("selectedHour"),
					b = this.get("selectedDate"),
					c = this.$().find(".hour-selector"),
					d = c.find("li").removeClass("active");
				null != a && (d.eq(a).addClass("active"), this._scrollIntoView(c, a), b && (b.setHours(a), this.updateMinutes(), this.selectedDateChange()))
			}),
			selectedMinuteChange: Ember.observer("selectedMinute", function() {
				var a = this.get("selectedMinute"),
					b = this.get("selectedDate"),
					c = this.$().find(".minute-selector"),
					d = c.find("li").removeClass("active");
				null != a && (d.eq(a).addClass("active"), this._scrollIntoView(c, a), b && (b.setMinutes(a), this.selectedDateChange()))
			}),
			selectValChange: Ember.observer("selectVal", function() {
				this.send("selectChange")
			}),

			setDefaultDate: function() {
				var a = this.get("defaultDate");
				a ? a = new Date(a) : (a = new Date(new Date - timeD_value), a.setMinutes(a.getMinutes() - 2)), this.set("selectedDate", a), this.updateBtnTxt()
			},
			_scrollIntoView: function(a, b) {
				a.scrollTop((a.find("li").height() || 26) * Math.max(b - 4, 0))
			},
			_initCalendar: function() {
				var a = this,
					b = this.get("selectedDate"),
					c = this.$().find(".calendar");
				c.wijcalendar({
					displayDate: b,
					maxDate: new Date,
					afterSelect: function(b, c) {
						a.set("selectedDate", c.date)
					}
				}), c.wijcalendar("selectDate", b)
			},
			_destroyCalendar: function() {
				var a = this.$().find(".calendar");
				a.data("wijmoWijcalendar") && a.wijcalendar("destroy")
			},
			updateBtnTxt: function() {

				var a = this.get("selectedDate");
				this.set("btnTxt", a ? APM.util.formatDate(a, "f") : null);
				var btnTxt = this.get("btnTxt");

				// $(".input-prepend button").text(btnTxt);
			},
			updateHours: function() {
				var a = new Date(this.get("selectedDate")),
					b = new Date(new Date() - timeD_value);;
					
				a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate() ? (this.set("maxHour", b.getHours()), this.get("selectedHour") > b.getHours() && this.send("hourChange", 0)) : this.set("maxHour", 23)
			},
			updateMinutes: function() {
				var a = new Date(this.get("selectedDate")),
					b = new Date(new Date() - timeD_value);;
				a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate() && a.getHours() == b.getHours() ? (this.set("maxMinute", b.getMinutes()), this.get("selectedMinute") > b.getMinutes() && this.send("minuteChange", 0)) : this.set("maxMinute", 59)
			},
			getTimeRange: function() {
				var a, b = this.get("selectedDate") || new Date(new Date() - timeD_value);
		
				switch (b.setSeconds(0), b.setMilliseconds(0),b = new Date(+b +60000),a = new Date(+b), this.get("selectVal")) {
					default:
						case "1m":
							a = new Date(+a - 60000);
					break;
					case "1h":
							a = new Date(+a - 3600000);
						break;
					case "n1d":
							a.setHours(0);
							a.setMinutes(0);
							a.setSeconds(0);
							var c = new Date(+a);
							c.setDate(c.getDate()+1);
							c.setHours(0);
							c.setMinutes(0);
							c.setSeconds(0);
							b = new Date(+c);
						break;
					case "n1M":
							a.setDate(1);
							a.setHours(0);
							a.setMinutes(0);
							a.setSeconds(0);
							var c = new Date(+a);
							c.setMonth(c.getMonth()+1);
							b = new Date(+c);

						break;
					case "1d":
							a.setDate(+a.getDate() - 1);

						break;
					case "7d":
							a.setDate(+a.getDate() - 7);

						break;
					case "1M":
							a.setMonth(+a.getMonth() - 1);

				}
				return {
					startDate: a,
					endDate: b,
					selectVal: this.get("selectVal")
				}
			},
			actions: {
				showStartDateSelector: function() {
					this._initCalendar(), this.$().find(".start-date-selector").show(), this._scrollIntoView(this.$().find(".hour-selector"), this.get("selectedHour")), this._scrollIntoView(this.$().find(".minute-selector"), this.get("selectedMinute"))
				},
				hideStartDateSelector: function() {
					this._destroyCalendar(), this.$().find(".start-date-selector").hide()
				},
				selectChange: function() {
					this.triggerAction({
						action: this.get("onChange"),
						actionContext: this.getTimeRange()
					})
				},
				hourChange: function(a) {
					this.set("selectedHour", a)
				},
				minuteChange: function(a) {
					this.set("selectedMinute", a)
				},
				btnOkAction: function() {
					this.updateBtnTxt(), this.send("selectChange"), this.send("hideStartDateSelector")
				},
				btnLastAction: function() {
					this.set("selectedDate", new Date(new Date - timeD_value)), this.send("btnOkAction")
				}
			}
		});
	return c
}), define("apm/ui/business_time_ranges", ["apm/ui/business_time_ranges/main"], function(a) {
	return a
});